<!--
 * @Author: luohongbiao
 * @Date: 2022-02-22 10:37:55
 * @LastEditTime: 2022-02-22 10:44:16
 * @LastEditors: luohongbiao1
 * @Description: 
 * @FilePath: \face_problem\1.js_base\3.变量提升.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        // 用var生名的变量会提升到作用域的顶部
        // 变量提升概念：用var声明的变量，会把变量提升到它所在的作用域的顶端去声明
        https://gitee.com/binarylhb/face_problem.git

        // 等价于test2

        function test2() {
            var a = undefined
            console.log(a)
            a = 111
        }
        test2()//undefined


        function test3() {
            a = 1
            var a
            console.log(a)
        }
        test3() // 1

        // 等价于test4
        var a = 1

        function test4() {
            var a
            a = 1
            console.log(a);
        }


        // 函数提升：，函数声明式可提升，字面量式不会
        bar()

        function bar() {
            console.log("bar")
        }

        // 函数字面量，不会
        try {
            test5()
        } catch (error) {
            console.log("没有test5函数啊")
            console.log(error)
        }
        var test5 = function() {
            console.log('test5')
        }
    </script>
</body>

</html>